package com.lin.mr.maizilaundry.myadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.entity.WaterfallInfo;

import java.util.List;

/**
 */
public class WaterfallAdapter extends RecyclerView.Adapter<WaterfallAdapter.ViewHolder> {
    private Context context;
    private List<WaterfallInfo> waterfallInfoList;
    private OnItemClickListener onItemClickListener;


    public interface OnItemClickListener{
        void OnItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener ){
        this.onItemClickListener = onItemClickListener;
    }

    public WaterfallAdapter(Context context, List<WaterfallInfo> waterfallInfoList) {
        this.context = context;
        this.waterfallInfoList = waterfallInfoList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.waterfall_item,null);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.waterfall_image.setImageResource(waterfallInfoList.get(position).getImage());
        holder.waterfall_name.setText(waterfallInfoList.get(position).getName());
        if (onItemClickListener != null){
            holder.waterfall_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.OnItemClick(holder.waterfall_image,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return waterfallInfoList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView waterfall_image;
        TextView waterfall_name;
        public ViewHolder(View itemView) {
            super(itemView);
            waterfall_image = (ImageView) itemView.findViewById(R.id.waterfall_image);
            waterfall_name = (TextView) itemView.findViewById(R.id.waterfall_name);
        }
    }
}
